﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;

using FileTreeSpace;
using ProgressSpace;
using TravPresenterAbstraction;
using TravModelViewSpace;

namespace TravModelSpace
{
    public partial class TravModel
    {
        public void SelectDuplicates ()
        {
            DuplWorkList workFiles = new DuplWorkList ();

            for (int di = 0; di < Detail.Presenter.Count; ++di)
            {
                string hashText = Detail.Presenter.Hash (di);
                if (hashText.Length == 0)
                    hashText = null;
                workFiles.Add (new DuplWorkItem (di, Detail.Presenter.FileSize (di), Detail.Presenter.FullName (di), hashText));
            }

            duplFinder.RunWorkerAsync (workFiles);

            Progress = Presenter.ProgressDialogFactory ("Finding duplicates");
            if (Progress != null)
            {
                ProgressResult result = Progress.Show ();
                if (result == ProgressResult.Cancel)
                    duplFinder.CancelAsync ();
                else
                    Detail.Presenter.Select ();
            }

            RefreshStatus ();
        }


        public void SelectFromPlaylist (string playlistName)
        {
            using (TextReader playlist = new StreamReader (playlistName))
            {
                int p = playlistName.LastIndexOf (@"\") + 1;
                string listPath = playlistName.Substring (0, p).ToLower ();

                for (; ; )
                {
                    string line = playlist.ReadLine ();
                    if (line == null)
                        break;

                    if (line.Length > 0 && line[0] != '#')
                        for (int i = 0; i < Detail.Presenter.Count; i++)
                        {
                            string fullName = Detail.Presenter.FullName (i).ToLower ();

                            line = line.ToLower ();

                            if (Path.IsPathRooted (line))
                            {
                                if (line == fullName)
                                    Detail.Presenter.SetIsSelected (i, true);
                            }
                            else
                                if (listPath + line.ToLower () == (Detail.Presenter.FullPathName (i) + @"\").ToLower () + Detail.Presenter.BaseName (i).ToLower ())
                                    Detail.Presenter.SetIsSelected (i, true);
                        }
                }
                Detail.Presenter.Select ();
            }
        }


        public void SaveSelectionToPlaylist (string playlistName, bool useAbsolutePath)
        {
            using (TextWriter playlist = new StreamWriter (playlistName))
            {
                int p = playlistName.LastIndexOf (@"\") + 1;
                string savePath = playlistName.Substring (0, p).ToLower ();

                foreach (string fullName in Detail.Presenter.SelectedFullNames)
                {
                    string saveName = fullName;

                    if (!useAbsolutePath)
                        if (fullName.Length >= p)
                            if (savePath == fullName.Substring (0, p).ToLower ())
                                saveName = fullName.Substring (p);

                    playlist.WriteLine (saveName);
                }
            }
        }

    }
}
